package com.xb.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xb.health.mapper.SysUserMapper;
import com.xb.health.pojo.SysUser;
import com.xb.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysUser> queryUsers() {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SysUser::getGender,1).like(SysUser::getUserName,"xx");
        return sysUserMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public SysUser queryUsername(String username) {
        //select * from sys_user where user_name = xxx
        LambdaQueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>().lambda();
//        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserName,username);
        return sysUserMapper.selectOne(queryWrapper);
    }

    @Override
    public void insert(SysUser user) {
        sysUserMapper.insert(user);
    }

    @Override
    public void update(SysUser user) {
        //update sys_user set real_name = xxx where phone = xxx
        LambdaUpdateWrapper<SysUser> lambdaUpdateWrapper = new LambdaUpdateWrapper();
        lambdaUpdateWrapper.set(SysUser::getRealName,user.getRealName());
        lambdaUpdateWrapper.eq(SysUser::getPhone,user.getPhone());
        sysUserMapper.update(user,lambdaUpdateWrapper);
    }

    @Override
    public void deleteUser(SysUser user) {
//        sysUserMapper.deleteById(user);
//        sysUserMapper.deleteById(user.getUid());
    }
}
